package com.kkwrite.regimen.consumer.controller.activity;

import com.kkwrite.regimen.common.entity.dto.OutDTO;
import com.kkwrite.regimen.common.entity.dto.activity.ActivityApplyInDTO;
import com.kkwrite.regimen.consumer.controller.order.OrderController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.kkwrite.regimen.common.constant.Constant.BOOLEAN_STRING_FALSE;
import static com.kkwrite.regimen.consumer.controller.constant.Constant.SERVICE_PREFIX;

/**
 * @author jinying
 */
@RestController
@RequestMapping(value = "/activity")
@Api(value = "活动模块")
public class ActivityController {
    @Resource(name = "stringRedisTemplate")
    private StringRedisTemplate redisTemplate;
    @Resource(name = "restTemplate")
    private RestTemplate restTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping(value = "/insertActivityApply")
    @ApiOperation(value = "新增活动报名")
    public OutDTO saveInviter(@RequestBody ActivityApplyInDTO inDTO,
                              HttpServletRequest request) {
        String token = request.getHeader("token");
        Object userObj = stringRedisTemplate.opsForHash().get(token, "userId");
        String openId = (String) stringRedisTemplate.opsForHash().get(token,
                "openId");
        if (userObj == null || openId == null) {
            return OutDTO
                    .of(BOOLEAN_STRING_FALSE, "002", "token invalid", null);
        }
        if (inDTO.getActivityCode() == null) {
            return OutDTO.of("faild", "活动编号必须参数");
        }
        if (inDTO.getName() == null) {
            return OutDTO.of("faild", "姓名必须参数");
        }
        if (inDTO.getPhone() == null) {
            return OutDTO.of("faild", "手机号必须参数");
        }
        inDTO.setId(null);
        OutDTO activityApplyOutDTO = restTemplate
                .postForObject(
                        SERVICE_PREFIX + "/activity/insertActivityApply/"
                                + Long.valueOf(userObj.toString()), inDTO,
                        OutDTO.class);
        Assert.notNull(activityApplyOutDTO, "");
        Map map = (Map) activityApplyOutDTO.getData();
        if ("0000".equals(activityApplyOutDTO.getSuccess())) {
            Map activityDTOmap = (Map) map.get("activityDTO");
            activityDTOmap.remove("success");
            activityDTOmap.remove("msg");
            return OutDTO.of("0000", "该用户已经参加此活动", null, activityDTOmap);
        }
        if ("0003".equals(activityApplyOutDTO.getSuccess())) {
            String fee = String
                    .valueOf((int) ((Double) map.get("price") * 100));
            String createIp = request.getHeader("X-Real-IP");
            OrderController orderController = new OrderController();
            String orderCode = (String) map.get("orderCode");
            return orderController.payUnifiedOrder(orderCode, fee, openId,
                    createIp);
        }

        Double price = (Double) map.get("price");
        String orderCode = (String) map.get("orderCode");
        String fee = String.valueOf((int) (price * 100));
        String createIp = request.getHeader("X-Real-IP");
        OrderController orderController = new OrderController();
        return orderController
                .payUnifiedOrder(orderCode, fee, openId, createIp);
    }

    @GetMapping(value = "/queryActivity/{activityCode}")
    @ApiImplicitParams({@ApiImplicitParam(name = "activityCode", value = "活动编号", required = true, dataType = "String", paramType = "path")})
    public OutDTO queryActivity(HttpServletRequest request,
                                @PathVariable String activityCode) {
        String userId = (String) redisTemplate.opsForHash().get(request.getHeader("token"), "userId");
        if (userId == null) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "002", "token invalid", null);
        }
        OutDTO out = restTemplate.getForObject(SERVICE_PREFIX + "/activity/queryActivity/" + activityCode
                        + "?userId=" + Long.valueOf(userId), OutDTO.class);
        if (out == null) {
            throw new RuntimeException();
        }
        Map activityDTOmap = (Map) out.getData();
        activityDTOmap.remove("success");
        activityDTOmap.remove("msg");
        return OutDTO.of(out.getSuccess(), out.getMsg(), null, activityDTOmap);
    }
}
